/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.framework.gen;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.star.framework.gen.model.ControllerModel;
import cn.star.framework.gen.model.EntityModel;
import cn.star.framework.gen.model.RepositoryModel;
import cn.star.framework.gen.model.ServiceImplModel;
import cn.star.framework.gen.model.ServiceModel;
import cn.star.framework.gen.util.TemplateUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * 代码生成器<br>
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
@Slf4j
public class Generator {

  /** 是否使用协议 */
  private final boolean license;

  /** 协议内容 */
  @Setter private String licenseText;

  public Generator() {
    this(false);
  }

  public Generator(boolean license) {
    this.license = license;
  }

  private String getLicenseText() {
    try {
      List<String> lines = new ArrayList<>();
      lines.add("/*");
      FileUtil.readUtf8Lines("LICENSE").forEach(i -> lines.add(StrUtil.format(" * {}", i)));
      lines.add(" */");

      return String.join("\n", lines);
    } catch (Exception e) {
      log.warn("read [LICENSE] content error, use default mit license, {}", e.getMessage());
      String mit =
          "/*\n"
              + " * MIT License\n"
              + " *\n"
              + " * Copyright (c) {}-2048 冰羽\n"
              + " *\n"
              + " * Permission is hereby granted, free of charge, to any person obtaining a copy\n"
              + " * of this software and associated documentation files (the \"Software\"), to deal\n"
              + " * in the Software without restriction, including without limitation the rights\n"
              + " * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n"
              + " * copies of the Software, and to permit persons to whom the Software is\n"
              + " * furnished to do so, subject to the following conditions:\n"
              + " *\n"
              + " * The above copyright notice and this permission notice shall be included in all\n"
              + " * copies or substantial portions of the Software.\n"
              + " *\n"
              + " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n"
              + " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n"
              + " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n"
              + " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n"
              + " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n"
              + " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n"
              + " * SOFTWARE.\n"
              + " */";
      return StrUtil.format(mit, DateUtil.format(new Date(), "YYYY"));
    }
  }

  private String wrapper(String code) {
    return license ? StrUtil.format("{}\n\n{}", this.getLicenseText(), code) : code;
  }

  /**
   * 生成{@link IEntity}子类代码
   *
   * @param model {@link EntityModel}
   * @return
   */
  public String getEntityCode(EntityModel model) {
    return this.wrapper(TemplateUtil.getEntityCode(model));
  }

  /**
   * 生成{@link IRepository}子类代码
   *
   * @param model {@link RepositoryModel}
   * @return
   */
  public String getRepositoryCode(RepositoryModel model) {
    return this.wrapper(TemplateUtil.getRepositoryCode(model));
  }

  /**
   * 生成{@link IService}子类代码
   *
   * @param model {@link ServiceModel}
   * @return
   */
  public String getServiceCode(ServiceModel model) {
    return this.wrapper(TemplateUtil.getServiceCode(model));
  }

  /**
   * 生成{@link AbstractService}、{@link #getServiceCode(ServiceModel)}子类代码
   *
   * @param model {@link ServiceImplModel}
   * @return
   */
  public String getServiceImplCode(ServiceImplModel model) {
    return this.wrapper(TemplateUtil.getServiceImplCode(model));
  }

  /**
   * 生成{@link AbstractController}子类代码
   *
   * @param model {@link ControllerModel}
   * @return
   */
  public String getControllerCode(ControllerModel model) {
    return this.wrapper(TemplateUtil.getControllerCode(model));
  }
}
